package com.shsoft.myThread;

import com.shsoft.colorfulcc.MainActivity;
import com.shsoft.common.Constant;
import com.shsoft.entity.CompositeItem;
import com.shsoft.utils.MessageUtil;

import org.litepal.LitePal;

import java.util.HashMap;
import java.util.List;

/**
 * 混色-复合模式 子线程
 */
public class MixCompositeThread extends Thread{
    private MainActivity activity;
    private List<CompositeItem> itemList;
    private int index; // 记录处理到哪一条步骤

    public MixCompositeThread(MainActivity activity) {
        this.activity = activity;
        itemList = LitePal.where("colorMode=?", Constant.COLOR_MODE_MIX + "").order("id asc").find(CompositeItem.class);
        index = 0;
    }

    @Override
    public void run() {
        System.out.println("混色复合模式子线程运行");
        while (activity.isPlaying){
            if(itemList == null || itemList.size() == 0){
                // 一个步骤也没有，什么也不做，空循环
            }else {
                CompositeItem item = itemList.get(index);
                // 获取item里的颜色编号
                String[] colors = item.getColorNum().split(",");
                if(item.getLightMode() == Constant.LIGHT_MODE_LONG){
                    int bright = item.getArg1();
                    if(bright * 2 > 255){
                        bright = 255;
                    }else {
                        bright = bright * 2;
                    }
                    int interval = item.getArg2() * 100;
                    int offtime = item.getArg3() * 100;
                    int circle = item.getArg4();
                    for(int j = 0; j < circle; j++){
                        if(activity.isPlaying){
                            // 将灯珠的颜色全部换成要显示的颜色
                            for(int m = 0; m < Constant.LIGHT_COUNT; m++){
                                int colorIndex = Integer.parseInt(colors[m].replace("#",""));
                                if(colorIndex == 20 || colorIndex >= activity.colors.size()){
                                    activity.lights_4display.put(m,new int[]{1,1,1});
                                }else {
                                    activity.lights_4display.put(m,activity.colors.get(colorIndex));
                                }
                            }
                            // 让主线程刷新灯珠区域
                            activity.alpha = bright;
                            activity.refreshLightsDiv();
                            // 睡眠 interval
                            try {
                                sleep(interval);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                                break;
                            }
                            if(offtime != 0){ // 需要灭灯
                                int rgb[] = new int[]{1,1,1};
                                for(int m = 0; m < Constant.LIGHT_COUNT; m++){
                                    activity.lights_4display.put(m,rgb);
                                }
                                // 让主线程刷新灯珠区域
                                activity.alpha = 250;
                                activity.refreshLightsDiv();
                                // 睡眠 interval
                                try {
                                    sleep(offtime);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                    break;
                                }
                            }
                        }
                    }
                }else if(item.getLightMode() == Constant.LIGHT_MODE_HORSE){
                    int bright = item.getArg1();
                    if(bright * 2 > 255){
                        bright = 255;
                    }else {
                        bright = bright * 2;
                    }
                    int direction = item.getArg2();
                    int speed = item.getArg3() * 10;
                    int circle = item.getArg4();
                    // 将灯珠的颜色全部换成要显示的颜色
                    for(int m = 0; m < Constant.LIGHT_COUNT; m++){
                        int colorIndex = Integer.parseInt(colors[m].replace("#",""));
                        if(colorIndex == 20 || colorIndex >= activity.colors.size()){
                            activity.lights_4display.put(m,new int[]{1,1,1});
                        }else {
                            activity.lights_4display.put(m,activity.colors.get(colorIndex));
                        }
                    }
                    for(int j = 0; j < circle * 20; j++){
                        if(activity.isPlaying){
                            // 每次把所有的颜色按顺时针或者逆时针变换
                            if(direction == Constant.CLOCKWISE){ // 顺时针
                                int[] rgb = activity.lights_4display.get(Constant.LIGHT_COUNT - 1);
                                for(int m = (Constant.LIGHT_COUNT - 1); m > 0 ; m--){
                                    activity.lights_4display.put(m,activity.lights_4display.get(m - 1));
                                }
                                activity.lights_4display.put(0,rgb);
                            }else { // 逆时针
                                int[] rgb = activity.lights_4display.get(0);
                                for(int m = 0; m < (Constant.LIGHT_COUNT - 1) ; m++){
                                    activity.lights_4display.put(m,activity.lights_4display.get(m + 1));
                                }
                                activity.lights_4display.put((Constant.LIGHT_COUNT - 1) ,rgb);
                            }
                            // 让主线程刷新灯珠区域
                            activity.alpha = bright;
                            activity.refreshLightsDiv();
                            try {
                                sleep(speed);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                                break;
                            }
                        }
                    }
                }else if(item.getLightMode() == Constant.LIGHT_MODE_FLOWING_WATER){
                    int bright = item.getArg1();
                    if(bright * 2 > 255){
                        bright = 255;
                    }else {
                        bright = bright * 2;
                    }
                    int direction = item.getArg2();
                    int speed = item.getArg3() * 10;
                    int circle = item.getArg4();
                    int mode = item.getArg5();
                    // 因为只写入若干个灯珠的数据，防止其他灯珠显示乱七八糟的的颜色，所以初始化 lights_4display
                    for(int i = 0; i <Constant.LIGHT_COUNT ; i++){
                        activity.lights_4display.put(i,new int[]{1,1,1});
                    }
                    for(int j = 0; j < circle; j++){ // 重复执行circle次
                        if(activity.isPlaying){
                            activity.alpha = bright;
                            for(int m = 0; m < 40; m++){
                                // 让主线程刷新灯珠区域
                                activity.refreshLightsDiv();
                                if(direction == Constant.CLOCKWISE) { // 顺时针
                                    if(mode == Constant.FLOWINGWATER_MODE_TAIL_FOLLOW){
                                        if(m < 20){
                                            int colorIndex = Integer.parseInt(colors[m].replace("#",""));
                                            if(colorIndex == 20 || colorIndex >= activity.colors.size()){
                                                activity.lights_4display.put(m,new int[]{1,1,1});
                                            }else {
                                                activity.lights_4display.put(m,activity.colors.get(colorIndex));
                                            }
                                        }else {
                                            activity.lights_4display.put((m - 20),new int[]{1,1,1});
                                        }
                                    }else if(mode == Constant.FLOWINGWATER_MODE_HEAD_BACK){
                                        if(m < 20){
                                            int colorIndex = Integer.parseInt(colors[m].replace("#",""));
                                            if(colorIndex == 20 || colorIndex >= activity.colors.size()){
                                                activity.lights_4display.put(m,new int[]{1,1,1});
                                            }else {
                                                activity.lights_4display.put(m,activity.colors.get(colorIndex));
                                            }
                                        }else {
                                            activity.lights_4display.put(19 - (m - 20),new int[]{1,1,1});
                                        }
                                    }else if(mode == Constant.FLOWINGWATER_MODE_FULL_FINISH){
                                        if(m == 0){
                                            for(int i = 0; i <Constant.LIGHT_COUNT ; i++){
                                                activity.lights_4display.put(i,new int[]{1,1,1});
                                            }
                                        }
                                        int colorIndex = Integer.parseInt(colors[m].replace("#",""));
                                        if(colorIndex == 20 || colorIndex >= activity.colors.size()){
                                            activity.lights_4display.put(m,new int[]{1,1,1});
                                        }else {
                                            activity.lights_4display.put(m,activity.colors.get(colorIndex));
                                        }
                                        if(m == 19){
                                            try {
                                                sleep(speed);
                                            } catch (InterruptedException e) {
                                                e.printStackTrace();
                                                break;
                                            }
                                            break;
                                        }
                                    }
                                }else { // 逆时针
                                    if(mode == Constant.FLOWINGWATER_MODE_TAIL_FOLLOW){
                                        if(m < 20){
                                            if(m == 0){
                                                int colorIndex = Integer.parseInt(colors[m].replace("#",""));
                                                if(colorIndex == 20 || colorIndex >= activity.colors.size()){
                                                    activity.lights_4display.put(0,new int[]{1,1,1});
                                                }else {
                                                    activity.lights_4display.put(0,activity.colors.get(colorIndex));
                                                }
                                            }else {
                                                int colorIndex = Integer.parseInt(colors[m].replace("#",""));
                                                if(colorIndex == 20 || colorIndex >= activity.colors.size()){
                                                    activity.lights_4display.put((20 - m),new int[]{1,1,1});
                                                }else {
                                                    activity.lights_4display.put((20 - m),activity.colors.get(colorIndex));
                                                }
                                            }
                                        }else {
                                            if(m == 20){
                                                activity.lights_4display.put(0, new int[]{1,1,1});
                                            }else {
                                                activity.lights_4display.put((40 - m),new int[]{1,1,1});
                                            }
                                        }
                                    }else if(mode == Constant.FLOWINGWATER_MODE_HEAD_BACK){
                                        if(m < 20){
                                            if(m == 0){
                                                int colorIndex = Integer.parseInt(colors[m].replace("#",""));
                                                if(colorIndex == 20 || colorIndex >= activity.colors.size()){
                                                    activity.lights_4display.put(0,new int[]{1,1,1});
                                                }else {
                                                    activity.lights_4display.put(0,activity.colors.get(colorIndex));
                                                }
                                            }else {
                                                int colorIndex = Integer.parseInt(colors[m].replace("#",""));
                                                if(colorIndex == 20 || colorIndex >= activity.colors.size()){
                                                    activity.lights_4display.put((20 - m),new int[]{1,1,1});
                                                }else {
                                                    activity.lights_4display.put((20 - m),activity.colors.get(colorIndex));
                                                }
                                            }
                                        }else {
                                            if(m == 39){
                                                activity.lights_4display.put(0, new int[]{1,1,1});
                                            }else {
                                                activity.lights_4display.put(m - 19,new int[]{1,1,1});
                                            }
                                        }
                                    }else if(mode == Constant.FLOWINGWATER_MODE_FULL_FINISH){
                                        if(m == 0){
                                            for(int i = 0; i <Constant.LIGHT_COUNT ; i++){
                                                activity.lights_4display.put(i,new int[]{1,1,1});
                                            }
                                            int colorIndex = Integer.parseInt(colors[m].replace("#",""));
                                            if(colorIndex == 20 || colorIndex >= activity.colors.size()){
                                                activity.lights_4display.put(0,new int[]{1,1,1});
                                            }else {
                                                activity.lights_4display.put(0,activity.colors.get(colorIndex));
                                            }
                                        }else {
                                            int colorIndex = Integer.parseInt(colors[m].replace("#",""));
                                            if(colorIndex == 20 || colorIndex >= activity.colors.size()){
                                                activity.lights_4display.put((20 - m),new int[]{1,1,1});
                                            }else {
                                                activity.lights_4display.put((20 - m),activity.colors.get(colorIndex));
                                            }
                                        }
                                        if(m == 19){
                                            try {
                                                sleep(speed);
                                            } catch (InterruptedException e) {
                                                e.printStackTrace();
                                                break;
                                            }
                                            break;
                                        }
                                    }
                                }
                                try {
                                    sleep(speed);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                    break;
                                }
                            }
                        }
                    }
                }else if(item.getLightMode() == Constant.LIGHT_MODE_BREATHING){
                    int bright = item.getArg1();
                    int time = item.getArg2();
                    int delay = (time * 1000) / (bright * 2);
                    int circle = item.getArg3();
                    int breathingMode = item.getArg4();
                    // 将灯珠的颜色全部换成要显示的颜色
                    for(int i = 0; i < Constant.LIGHT_COUNT; i++){
                        int colorIndex = Integer.parseInt(colors[i].replace("#",""));
                        if(colorIndex == 20 || colorIndex >= activity.colors.size()){
                            activity.lights_4display.put(i,new int[]{1,1,1});
                        }else {
                            activity.lights_4display.put(i,activity.colors.get(colorIndex));
                        }
                    }
                    for(int j = 0; j < circle; j++) { // 重复执行circle次
                        if(activity.isPlaying){
                            if(breathingMode == Constant.BREATHING_MODE_D_L_D){
                                for (int m = 1; m < (bright * 2); m++){
                                    // 让主线程刷新灯珠区域
                                    if(m <= bright){
                                        activity.alpha = m;
                                    }else {
                                        activity.alpha = bright - (m - bright);
                                    }
                                    if(activity.alpha * 2 > 255){
                                        activity.alpha = 255;
                                    }else {
                                        activity.alpha = activity.alpha * 2;
                                    }
                                    activity.refreshLightsDiv();
                                    try {
                                        sleep(delay);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                        break;
                                    }
                                }
                            }else if(breathingMode == Constant.BREATHING_MODE_L_D_L){
                                for (int m = 1; m < (bright * 2); m++){
                                    // 让主线程刷新灯珠区域
                                    if(m <= bright){
                                        activity.alpha = (bright + 1) - m;
                                    }else {
                                        activity.alpha = m - (bright - 1);
                                    }
                                    if(activity.alpha * 2 > 255){
                                        activity.alpha = 255;
                                    }else {
                                        activity.alpha = activity.alpha * 2;
                                    }
                                    activity.refreshLightsDiv();
                                    try {
                                        sleep(delay);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                        break;
                                    }
                                }
                            }else if(breathingMode == Constant.BREATHING_MODE_L_D){
                                for (int m = 1; m <= bright; m++){
                                    // 让主线程刷新灯珠区域
                                    activity.alpha = (bright + 1) - m;
                                    if(activity.alpha * 2 > 255){
                                        activity.alpha = 255;
                                    }else {
                                        activity.alpha = activity.alpha * 2;
                                    }
                                    activity.refreshLightsDiv();
                                    try {
                                        sleep(delay * 2);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                        break;
                                    }
                                }
                            }else if(breathingMode == Constant.BREATHING_MODE_D_L){
                                for (int m = 1; m <= bright; m++){
                                    // 让主线程刷新灯珠区域
                                    activity.alpha = m;
                                    if(activity.alpha * 2 > 255){
                                        activity.alpha = 255;
                                    }else {
                                        activity.alpha = activity.alpha * 2;
                                    }
                                    activity.refreshLightsDiv();
                                    try {
                                        sleep(delay * 2);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
                index++;
                if(index == itemList.size()){
                    index = 0;
                }
            }
        }
    }
}
